package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.modular.dto.UserDTO;
import cn.stylefeng.guns.modular.system.dao.AppUserInfoMapper;
import cn.stylefeng.guns.modular.system.dao.ProxyUserMapper;
import cn.stylefeng.guns.modular.system.entity.AppUserInfo;
import cn.stylefeng.guns.modular.system.entity.AppUserInfoRes;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.exception.ProjectException;
import cn.stylefeng.guns.modular.system.service.AppUserInfoService;
import cn.stylefeng.guns.modular.system.vo.UserInfoVO;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.util.MD5Util;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AppUserInfoServiceImpl
 * @Author lzy
 * @Date 2020/1/11 16:10
 * @VERSION 1.0
 **/
@Service
public class AppUserInfoServiceImpl extends ServiceImpl<AppUserInfoMapper, AppUserInfo> implements AppUserInfoService {

    @Autowired
    private ProxyUserMapper proxyUserMapper;

    @Override
    public List<AppUserInfoRes> findAppUserInfo(String startTime, String endTime, String account, String phone, List<String> subUserIds) {
        if(!ObjectUtils.isEmpty(account)){
            account = account + "%";
        }
        if(!ObjectUtils.isEmpty(phone)){
            phone = phone + "%";
        }
        List<AppUserInfoRes> appUserInfos = baseMapper.findAppUserInfo(startTime, endTime, account, phone, subUserIds);
        return appUserInfos;
    }
    @Override
    public User selectUserById(Long superiorAgentId) {
        return proxyUserMapper.selectById(superiorAgentId);
    }

    @Override
    public AppUserInfo selectAppUserInfo(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateAppUserInfo(UserDTO build) {
        baseMapper.updateAppUserInfo(build);
    }

    @Override
    @Transactional
    public void updateRate(Integer id, BigDecimal exchangeRate) {
        BigDecimal feilv = limitLength(exchangeRate);
        baseMapper.updateRate(id,feilv);
    }

    @Override
    public Integer getAccountCount(String phone) {
        return baseMapper.getAccountCount(phone);
    }

    @Override
    public Integer getEmailCount(String email) {
        return baseMapper.getEmailCount(email);
    }

    @Override
    public List<String> findUserIdsByProxyId(String proxyId) {
        return baseMapper.findUserIdsByProxyId(proxyId);
    }

    @Override
    public List<AppUserInfo> selectUserInfoByUserId(String userId) {
        return baseMapper.selectUserInfoByUserId(userId);
    }


    @Override
    @Transactional
    public void insertUser(UserInfoVO userInfoVO) {
        BigDecimal feilv = limitLength(userInfoVO.getExchangeRate());
        //根据上级代理id查询开用户的代理
        User user = this.selectUserById(userInfoVO.getProxyId());
        //自己的费率
        BigDecimal exchangeRate = user.getExchangeRate();
        //小比大-1    大比小1   一样大0  限制设置的费率只能比自己小
        if(exchangeRate.compareTo(feilv) != 1){
            throw new ProjectException(ResultEnum.OVERSIZED_RATES);
        }
        String newPassword = MD5Util.encrypt(MD5Util.encrypt(userInfoVO.getUserPwd()));
        UserDTO build = UserDTO.builder()
                .account(userInfoVO.getPhone())
                .nickName(userInfoVO.getRealName())
                .balance(new BigDecimal(0))
                .createTime(new Date())
                .creditScore(0)
                .email(userInfoVO.getEmail())
                .exchangeRate(feilv)
                .idCard(userInfoVO.getIdCard())
                .lastAliTime(new Date())
                .lastWxTime(new Date())
                .nickName(userInfoVO.getNickName())
                .orderBy(0)
                .phone(userInfoVO.getPhone())
                .proxyId(userInfoVO.getProxyId())
                .realName(userInfoVO.getRealName())
                .status("0")
                .userPwd(newPassword)
                .build();
        this.baseMapper.insertUser(build);

    }


    @Override
    @Transactional
    public void deleteUserInfo(Long id) {
        baseMapper.deleteUserInfo(id);
    }



    /**
     * 限制费率长度
     * @param feilv
     * @return
     */
    public BigDecimal limitLength(BigDecimal feilv){
        //限制传过来的数据小数点只能有一位
        BigDecimal bigDecimal = feilv.setScale(1, BigDecimal.ROUND_DOWN);
        double value = bigDecimal.doubleValue();
        //设置费率不能超过1.5改为2.0，最低不能低于0.1
        if(value >2.0 || value < 0.1){
            throw new ProjectException(ResultEnum.RATE_IS_ABNORMAL);
        }
        return bigDecimal;
    }
}
